<template>
  <div class="tabs">
    <el-tag v-for="(item, index) in tags"
            :key="item.path"
            :closable="item.name !== 'home'"
            :effect="$route.name === item.name ? 'dark' : 'plain'"
            @click="changeMenu(item)"
            @close="handleClose(item, index)"
            size="small">
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
export default {
  setup () {
    const store = useStore();
    const router = useRouter();
    const route = useRoute();
    var tags = store.state.tabsList;

    //点击事件 实现路由跳转
    const changeMenu = (item) => {
      router.push({
        name: item.name,
      })
    };
    //关闭tag
    const handleClose = (item, index) => {
      let length = tags.length - 1;
      store.commit('closeTab', item);
      //做第一个判断 
      if (item.name !== route.name) {
        return;
      }
      if (index === length) {
        router.push({
          name: tags[index - 1].name,
        })
      } else {
        router.push({
          name: tags[index].name,
        })
      }

    };
    return {
      tags,
      changeMenu,
      handleClose
    };
  }
}
</script>

<style lang="less" scoped>
.tabs {
  margin-top: 15px;
  margin-left: 20px;
  height: 15px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>
